<?php

namespace app\controllers;

use yii\data\Pagination;
use yii\db\Query;
use yii\web\Controller;


class CompanyController extends AdminBaseController {

      //企业列表
     public function actionIndex(){
         if ($this->post()){
             $name=\Yii::$app->request->post()['name'];
             $query=(new Query())->select('*')->from('yii_company')->where(['like','name',$name]);
         }else{
             $query=(new Query())->select('*')->from('yii_company');
         }

         $pages=new Pagination(['totalCount' =>$query->count()]);
         $pages->defaultPageSize = 10;//每页10条
         $list=$query->offset($pages->offset)->limit($pages->limit)->all();

         foreach ($list as $k=>$v){
             $list[$k]['img']=$this->GetImg($v['logo']);
             $list[$k]['industry']=$this->GetIndustry($v['industry']);
             $list[$k]['number']=$this->GetNumber($v['number']);
         }
         return $this->render('index',['list'=>$list,'pages'=>$pages]);
       }


       //添加企业
      public function actionAdd(){
          if ($this->post()){
              $transaction = \Yii::$app->db->beginTransaction();
              try {
              $data=\Yii::$app->request->post()['company'];
              $data['create_time']=time();
              \Yii::$app->db->createCommand()->insert('yii_company',$data)->execute();
              $compay_id=\Yii::$app->db->getLastInsertID();
              if (isset(\Yii::$app->request->post()['img_id'])){
                  $img=\Yii::$app->request->post()['img_id'];
                  foreach ($img as $v){
                      $dataimg['company_id']=$compay_id;
                      $dataimg['img_id']=$v;
                      \Yii::$app->db->createCommand()->insert('yii_company_image',$dataimg)->execute();
                  }
              }
                  $transaction->commit();
                  return json_encode(['code'=>1,'msg'=>'添加成功']);
              } catch (\Exception $e) {
                  $transaction->rollBack();
                  return json_encode(['code'=>0,'msg'=>'添加失败']);

              }
          }
          $industry=(new Query())->select('*')->from('yii_industry')->all();
          $financing=(new Query())->select('*')->from('yii_financing')->all();
          $number=(new Query())->select('*')->from('yii_number')->all();
          return $this->render('add',['industry'=>$industry,'financing'=>$financing,'number'=>$number]);
      }

    //编辑企业
    public function actionEdit(){
        if ($this->post()){
            $transaction = \Yii::$app->db->beginTransaction();
            try {
                $data=\Yii::$app->request->post()['company'];
                \Yii::$app->db->createCommand()->update('yii_company',$data,array('id'=>$data['id']))->execute();

                if (isset(\Yii::$app->request->post()['img_id'])){
                    $img=\Yii::$app->request->post()['img_id'];
                    foreach ($img as $v){
                        $dataimg['company_id']=$data['id'];
                        $dataimg['img_id']=$v;
                        \Yii::$app->db->createCommand()->insert('yii_company_image',$dataimg)->execute();
                    }
                }
                $transaction->commit();
                return json_encode(['code'=>1,'msg'=>'更新成功']);
            } catch (\Exception $e) {
                $transaction->rollBack();
                return json_encode(['code'=>0,'msg'=>'更新失败']);

            }
        }
        $id=$this->GetId();
        $list=$this->GetCompany($id);//获取企业信息

        $img=(new Query())->select('*')->from('yii_company_image')->where(array('company_id'=>$id))->all();
        foreach ($img as $k=>$v){
            $img[$k]['img']=$this->GetImg($v['img_id']);
        }
        $industry=(new Query())->select('*')->from('yii_industry')->all();
        $financing=(new Query())->select('*')->from('yii_financing')->all();
        $number=(new Query())->select('*')->from('yii_number')->all();
        return $this->render('edit',['industry'=>$industry,'financing'=>$financing,'list'=>$list,'img'=>$img,'number'=>$number]);
    }


    //删除企业
    public function actionDel(){
         $id=$this->PostId();
         $transaction = \Yii::$app->db->beginTransaction();
         try {
             $company=$this->GetCompany($id);
             $this->DelFile($company['logo']);
             $this->DelPicture($company['logo']);

             \Yii::$app->db->createCommand()->delete('yii_company',array('id'=>$id))->execute();
             \Yii::$app->db->createCommand()->delete('yii_work',array('company'=>$id))->execute();
             $info=(new Query())->select('*')->from('yii_company_image')->where(array('company_id'=>$id))->all();
             \Yii::$app->db->createCommand()->delete('yii_company_image',array('company_id'=>$id))->execute();
             foreach ($info as $v){
                 $this->DelFile($v['img_id']);
                 $this->DelPicture($v['img_id']);
            }
            $transaction->commit();
            return json_encode(['code'=>1,'msg'=>'删除成功']);
        } catch (\Exception $e) {
            $transaction->rollBack();
            return json_encode(['code'=>0,'msg'=>'删除失败']);
        }
    }

    //删除所有全选企业
    public function actionDelall(){
         $id=\Yii::$app->request->post()['id'];
         $arr=explode(',',$id);
        $transaction = \Yii::$app->db->beginTransaction();
        try {
         foreach ($arr as $v2){
                 $company=$this->GetCompany($v2);
                 $this->DelFile($company['logo']);
                 $this->DelPicture($company['logo']);

                 \Yii::$app->db->createCommand()->delete('yii_company',array('id'=>$v2))->execute();
                 \Yii::$app->db->createCommand()->delete('yii_work',array('company'=>$v2))->execute();
                 $info=(new Query())->select('*')->from('yii_company_image')->where(array('company_id'=>$v2))->all();
                 \Yii::$app->db->createCommand()->delete('yii_company_image',array('company_id'=>$v2))->execute();
                 foreach ($info as $v){
                     $this->DelFile($v['img_id']);
                     $this->DelPicture($v['img_id']);
                 }
         }
            $transaction->commit();
            return json_encode(['code'=>1,'msg'=>'删除成功']);
        } catch (\Exception $e) {
            $transaction->rollBack();
            return json_encode(['code'=>0,'msg'=>'删除失败']);
        }
    }

    //删除图片
    public function actionDelimg(){
        $id=$this->PostId();
        $transaction = \Yii::$app->db->beginTransaction();
        try {
            $info=(new Query())->select('*')->from('yii_company_image')->where(array('id'=>$id))->one();
            \Yii::$app->db->createCommand()->delete('yii_company_image',array('id'=>$id))->execute();
            $this->DelFile($info['img_id']);
            $this->DelPicture($info['img_id']);
            $transaction->commit();
            return json_encode(['code'=>1,'msg'=>'删除成功']);
        } catch (\Exception $e) {
            $transaction->rollBack();
            return json_encode(['code'=>0,'msg'=>'删除失败']);
        }
    }

    //获取企业信息
    public function GetCompany($id){
        $list=(new Query())->select('*')->from('yii_company')->where(array('id'=>$id))->one();
        $list['img']=$this->GetImg($list['logo']);
        return $list;
    }

    //行业类别
    public function GetIndustry($id){
        $list=(new Query())->select('name')->from('yii_industry')->where(array('id'=>$id))->one();
        return $list['name'];
    }

    //企业人数
    public function GetNumber($id){
        $list=(new Query())->select('name')->from('yii_number')->where(array('id'=>$id))->one();
        return $list['name'];
    }


}
